package cn.endeavour.product.controller;


import cn.endeavour.basic.util.AjaxResult;
import cn.endeavour.basic.util.PageList;
import cn.endeavour.product.domain.Product;
import cn.endeavour.product.query.ProductQuery;
import cn.endeavour.product.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private IProductService productService;

    /**
     * 批量上架
     * @param ids
     * @return
     */
    @PatchMapping("/onSale")
    public AjaxResult onSale(@RequestBody List<Long> ids){
        try {
            productService.onSale(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("操作失败，请稍后重试！");
        }
    }

    /**
     * 批量下架
     * @param ids
     * @return
     */
    @PatchMapping("/offSale")
    public AjaxResult offSale(@RequestBody List<Long> ids){
        System.out.println(ids);
        try {
            productService.offSale(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("操作失败，请稍后重试！");
        }
    }

    /**
     * 添加或修改数据
     * 需要连表操作，detail和product
     * @param product
     * @return
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Product product){
        try {
            if (product.getId() != null) {
                productService.update(product);
            }else{
                productService.add(product);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            return  AjaxResult.me().setMsg("操作失败！！");
        }
    }

    /**
     * 根据id查询服务详情
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Product findById(@PathVariable("id") Long id){
        return productService.findById(id);
    }

    /**
     * 删除数据
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult deleteByID(@PathVariable("id") Long id){
        try {
            productService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            return AjaxResult.me().setMsg("操作失败！");
        }
    }


    /**
     * 查询所有产品数据
     * @return
     */
    @GetMapping
    public List<Product> findAll(){
        return productService.findAll();
    }

    /**
     * 分页查询
     * @param query
     * @return
     */
    @PostMapping
    public PageList<Product> queryPage(@RequestBody ProductQuery query){
        return productService.queryPage(query);
    }


    /**
     * 分页查询--普通用户查询，只能看到上架服务
     * @param query
     * @return
     */
    @PostMapping("/onSale")
    public PageList<Product> queryOnSale(@RequestBody ProductQuery query){
        return productService.queryPage(query);
    }



    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    public AjaxResult patchRemove(@RequestBody List<Long> ids){
        try {
            productService.patchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setMsg("操作失败！");
        }
    }
}
